﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using Telerik.WinControls.UI;
using Telerik.WinControls;
using System.IO;
using System.Collections;
using QuanLyNhaSach.Report;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace QuanLyNhaSach
{
    public partial class frmBC_TongHopDoanhThuBanHang : Form
    {
        public frmBC_TongHopDoanhThuBanHang()
        {
            InitializeComponent();
        }

        //Tao Parttern Seleton
        static frmBC_TongHopDoanhThuBanHang doituong = null;        
        public static frmBC_TongHopDoanhThuBanHang TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmBC_TongHopDoanhThuBanHang();
            return doituong;
        }
        #region "Khai Báo Biến"
        ColumnGroupsViewDefinition columnGroupsView;
        
        sv_QuanLyNhaSach.AdapterClient sv_QLNS = new sv_QuanLyNhaSach.AdapterClient();
        private ILog logger;
        DataSet ds_BCTon;
        ArrayList kiem_Tra_DSKho;
        //Khai Bao Bien Truyen Vao Store
         DateTime TuNgay;
         DateTime DenNgay;
         string MaKho;
         string MaMH;
         #endregion
        
        private void frmBC_TongHopDoanhThuBanHang_Load(object sender, EventArgs e)
        {
            dtpTuNgay.Value = DateTime.Now;
            dtpDenNgay.Value = DateTime.Now;
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            //Load Danh Sach Kho
            Load_DSKho();

            //Load Danh Sach Mat Hang
            Load_DSMatHang();


            this.gvxuatnhapton.EnableFiltering = false;
            this.gvxuatnhapton.ShowFilteringRow = false;
            this.gvxuatnhapton.ShowHeaderCellButtons = true;
            // column groups view        
            this.columnGroupsView = new ColumnGroupsViewDefinition();

            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("STT"));
            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("Mã Hàng"));
            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("Tên Hàng"));
            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("Số Lượng Tồn Đầu Kỳ"));
            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("Số Lượng Tồn Trong Kỳ"));
            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("Số Lượng Tồn Cuối Kỳ"));
            this.columnGroupsView.ColumnGroups.Add(new GridViewColumnGroup("Tổng Tồn"));


            this.columnGroupsView.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView.ColumnGroups[0].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["STT"]);
            this.columnGroupsView.ColumnGroups[1].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView.ColumnGroups[1].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["MaMH"]);
            this.columnGroupsView.ColumnGroups[2].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView.ColumnGroups[2].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["TenMH"]);
            this.columnGroupsView.ColumnGroups[3].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView.ColumnGroups[3].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["SoLuongDauKy"]);
            this.columnGroupsView.ColumnGroups[3].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["SLNhapDauKy"]);
            this.columnGroupsView.ColumnGroups[3].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["SLXuatDauKy"]);
            this.columnGroupsView.ColumnGroups[4].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView.ColumnGroups[4].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["SLNhapTrongKy"]);
            this.columnGroupsView.ColumnGroups[4].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["SLXuatTrongKy"]);
            this.columnGroupsView.ColumnGroups[5].Rows.Add(new GridViewColumnGroupRow());            
            this.columnGroupsView.ColumnGroups[5].Rows[0].Columns.Add(this.gvxuatnhapton.Columns["TongSLTon"]);



            this.gvxuatnhapton.ViewDefinition = columnGroupsView;
            this.gvxuatnhapton.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;                         
        }

        private void Load_DSMatHang()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(sv_QLNS.LayDS_MatHang()));
                if (ds.Tables.Count > 0)
                {
                    mcbb_MatHang.DataSource = ds.Tables[0];
                    mcbb_MatHang.ValueMember = "MaMH";
                    mcbb_MatHang.DisplayMember = "TenMH";
                    mcbb_MatHang.SelectedIndex = -1;                    
                }
            }
            catch (Exception ex)
            {

                logger.Error("Error: " + ex.Message);
            }
        }

        private void Load_DSKho()
        {
            try
            {                
                DataSet ds = new DataSet();
                ds.ReadXml(new StringReader(sv_QLNS.LayDS_Kho()));
                if (ds.Tables.Count > 0)
                {
                    mcbb_Kho.DataSource = ds.Tables[0];
                    mcbb_Kho.ValueMember = "MaKho";
                    mcbb_Kho.DisplayMember = "TenKho";
                    mcbb_Kho.SelectedIndex = -1;
                    //Dua Danh Sach ten Kho vao Test
                    kiem_Tra_DSKho = new ArrayList();
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        kiem_Tra_DSKho.Add(ds.Tables[0].Rows[i]["TenKho"].ToString().Trim());
                    }
                }
            }
            catch (Exception ex)
            {

                logger.Error("Error: " + ex.Message);
            }            
        }

        private void btn_Loc_Click(object sender, EventArgs e)
        {
            try
            {
                if(!kiem_Tra_DSKho.Contains(mcbb_Kho.Text.Trim()))
                {
                    MessageBox.Show("Vui lòng nhập đúng thông tin kho hàng.","Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    mcbb_Kho.Focus();
                    return;
                }
                if (dtpTuNgay.Value > dtpDenNgay.Value)
                {
                    MessageBox.Show("Từ Ngày phải bằng hoặc nhỏ hơn Đến Ngày. Vui lòng chọn lại Từ Ngày.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    dtpTuNgay.Focus();
                    return;
                }
                //Khai bao bien truyen vao Store
                TuNgay = dtpTuNgay.Value;
                DenNgay = dtpDenNgay.Value;
                MaKho = mcbb_Kho.SelectedValue.ToString();
                
                if (mcbb_MatHang.Text.Trim() == "")
                    MaMH = "";
                else
                    MaMH = mcbb_MatHang.SelectedValue.ToString();

                ds_BCTon = new DataSet();
                ds_BCTon.ReadXml(new StringReader(sv_QLNS.BaoCaoXuatNhapTon(TuNgay,DenNgay,MaKho,MaMH)));
                if (ds_BCTon.Tables.Count > 0)
                {
                    gvxuatnhapton.DataSource = ds_BCTon.Tables[0];
                    //Them Du lieu trong Danh Sach Mat Hang
                    mcbb_MatHang.DataSource = ds_BCTon.Tables[0];
                    mcbb_MatHang.DisplayMember = "TenMH";
                    mcbb_MatHang.ValueMember = "MaMH";
                    //Them gia tri cho Cot STT
                    for (int i = 0; i < gvxuatnhapton.Rows.Count; i++)
                    {
                        gvxuatnhapton.Rows[i].Cells["STT"].Value = i + 1;
                    }
                }
            }
            catch (Exception ex)
            {

                logger.Error("Error: " + ex.Message);
            }
        }

        private void btn_XemIn_Click(object sender, EventArgs e)
        {
            frmRP_BC_XuatNhapTon xuatnhapton = new frmRP_BC_XuatNhapTon();
            xuatnhapton.TuNgay = TuNgay;
            xuatnhapton.DenNgay = DenNgay;
            xuatnhapton.MaKho = MaKho;
            xuatnhapton.MaMH = MaMH;
            xuatnhapton.ShowDialog();           
        }

        private void mcbb_Kho_SelectedIndexChanged(object sender, EventArgs e)
        {            
            mcbb_MatHang.Text = "";
        }

        private void bt_TroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
